// 导入ws模块
const WebSocket = require("ws");

const port = 8501;

// 创建一个WebSocket服务器，绑定到 port 端口
const wss = new WebSocket.Server({ port });

// 当有客户端连接时触发
wss.on("connection", (ws) => {
  console.log("客户端已连接");

  // 接收客户端发来的消息
  ws.on("message", (message) => {
    console.log(`接收到的消息: ${message}`);

    // 向客户端发送消息
    ws.send(`服务端回应: ${message}`);
  });

  // 当连接关闭时触发
  ws.on("close", () => {
    console.log("客户端已断开连接");
  });
});

// 发送消息
function broadcast(message) {
  wss.clients.forEach((client) => {
    if (client.readyState === WebSocket.OPEN) {
      client.send(message);
    }
  });
}

const data = {};

// 设置定时器，每隔指定的时间（例如，每5秒）向所有客户端发送消息
const interval = setInterval(() => {
  // const message = "这是来自服务端的定时消息";
  const message = JSON.stringify({
    src: "snap",
    msg: "success",
    body: {
      time: 0,
      image: "/images/202406/20240621/20240621113022_ik2vrx.jpg",
      statistics: {
        tag: [
          {
            tag: "压伤",
            num: 2,
          },
          {
            tag: "缺料",
            num: 7,
          },
        ],
        ok: 0,
        ng: 1,
      },
      image_render: "/images/202406/20240621/20240621113022_ik2vrx.jpg",
      blob: [
        {
          angle: -89.605,
          x: 3233.31525,
          width: 160.976,
          score: 1,
          tag: "压伤",
          y: 2025.3935,
          height: 34.406,
          area: 2180,
        },
        {
          angle: -52.125,
          x: 1176.938,
          width: 44.151,
          score: 1,
          tag: "缺料",
          y: 842.50675,
          height: 39.941,
          area: 1232,
        },
        {
          angle: 0,
          x: 2889,
          width: 70,
          score: 1,
          tag: "缺料",
          y: 47.25,
          height: 27,
          area: 1162,
        },
        {
          angle: 59.237,
          x: 3788.81375,
          width: 73.897,
          score: 1,
          tag: "缺料",
          y: 462.425,
          height: 30.768,
          area: 1140,
        },
        {
          angle: -49.574,
          x: 3252.92925,
          width: 49.945,
          score: 1,
          tag: "压伤",
          y: 2111.17,
          height: 42.164,
          area: 995,
        },
        {
          angle: -3.814,
          x: 2962.06175,
          width: 47.231,
          score: 1,
          tag: "缺料",
          y: 3079.65925,
          height: 25.479,
          area: 680,
        },
        {
          angle: -45.807,
          x: 3877.3425,
          width: 53.938,
          score: 1,
          tag: "缺料",
          y: 505.4365,
          height: 20.678,
          area: 576,
        },
        {
          angle: -76.759,
          x: 3960.783,
          width: 39.937,
          score: 1,
          tag: "缺料",
          y: 573.79175,
          height: 18.865,
          area: 569,
        },
        {
          angle: -66.038,
          x: 3976.6405,
          width: 34.811,
          score: 1,
          tag: "缺料",
          y: 893.5835,
          height: 20.698,
          area: 460,
        },
      ],
      status: "NG",
      device_no: "66849376",
      imageWidth: 5472,
      imageHeight: 3648,
    },
    err: 0,
  });
  // console.log(`正在发送消息: ${message}`);
  broadcast(message);
}, 5000); // 5000毫秒即5秒

// 添加一个监听器以优雅地处理程序退出，清除定时器
process.on("SIGINT", function () {
  clearInterval(interval);
  console.log("\nGracefully shutting down from SIGINT (Ctrl-C)");
  process.exit();
});

console.log(`WebSocket服务器正在监听 ${port} 端口...`);

module.exports = wss;
